package leetcode.dichotomy.demoa;

import java.util.Arrays;

/**
 * @author cyt
 * @create 2020-09-06 15:47
 */
public class Td {
    public static void main(String[] args) {
        Td td = new Td();
        int[] ints = {4, 5, 6, 7, 0, 1, 2};
        System.out.println(td.search(ints, 3));
    }

    public int search(int[] nums, int target) {
        int[] ints = Arrays.copyOf(nums, nums.length);
        Arrays.sort(nums);
        int l = 0;
        int r = nums.length - 1;
        int ans = -1;
        while (l <= r) {
            int mid = l + (r - l) / 2;
            if (nums[mid] == target) {
                ans = nums[mid];
                break;
            } else if (nums[mid]  > target) {
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        if (ans == -1) {
            return ans;
        }
        for (int i = 0; i < ints.length; i++) {
            if (ans == ints[i]) {
                ans = i;
                break;
            }
        }
        return ans;
    }
}

